
#include "musicMgr_t.h"

void musicMgr_t::setMain(const string & file)
{
	HGE * hge = hgeCreate( HGE_VERSION );
	hge->Stream_Free(backSound);
	backSound=hge->Stream_Load(file.c_str());
	backChannel = hge->Stream_Play(backSound,true,curMainVolume);
}


void musicMgr_t::addSound(const string & file)
{
	HGE * hge = hgeCreate( HGE_VERSION );
	HSTREAM snd=hge->Stream_Load(file.c_str());
	HCHANNEL cha = hge->Stream_Play(snd,false,curSoundVolume);
	sounds.insert(make_pair(snd,cha));
}


void musicMgr_t::update()
{
	HGE * hge = hgeCreate( HGE_VERSION );
	mapSound::iterator iter;
_begin:
	for (iter=sounds.begin(); iter!=sounds.end(); iter++)
	{
		if (!hge->Channel_IsPlaying(iter->second))
		{
			hge->Stream_Free(iter->first);
			sounds.erase(iter);
			goto _begin;
		}
	}
}


void musicMgr_t::setMainVolume(int volume)
{ 
	if (volume<0)volume=0;
	if (volume>100)volume=100;
	this->curMainVolume=volume; 
	HGE * hge = hgeCreate( HGE_VERSION );
	hge->Channel_SetVolume(backChannel, volume);
}


int musicMgr_t::getMainVolume()
{
	return curMainVolume;
}

void musicMgr_t::setSoundVolume(int volume)
{ 
	if (volume<0)volume=0;
	if (volume>100)volume=100;
	this->curSoundVolume=volume; 

	HGE * hge = hgeCreate( HGE_VERSION );
	mapSound::iterator iter;
	for (iter=sounds.begin(); iter!=sounds.end(); iter++)
	{
		hge->Channel_SetVolume(iter->second, volume);
	}
}

int musicMgr_t::getSoundVolume()
{
	return curSoundVolume;
}